package {
	import b1.*;
	import b1.conifg.*;
	import b1.controls.basic.*;
	import b1.net.*;
	import b1.players.*;
	import b1.scenes.SceneContainer;
	import b1.scenes.entry.*;
	import b1.scenes.load.*;
	import b1.scenes.main.*;
	import b1.utils.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.external.*;
	import flash.text.*;

	[SWF(backgroundColor="#FFFFFF", frameRate="60", quality="MEDIUM", width="800", height="600")]
	public class B1 extends Sprite
	{
		private var testSprite:MovieClip;
		private var loadBar:LoadProgressBar;
		
		public function B1()
		{
			SceneContainer.getInstance().debug("B1");
			addChild(SceneContainer.getInstance());
			var loadingScene:LoadingScene = new LoadingScene("正在加载主资源……");
			SceneContainer.getInstance().loadScene(loadingScene);
			ResourceHelper.getInstance().startLoadMainResource(loadingScene.loadBar.updateProgress, main);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
		}
		
		public function onKeyDown(e:KeyboardEvent):void 
		{
			switch (e.keyCode) 
			{
				case 113:
					SceneContainer.getInstance().switchDebug();
					break;
				case 33:
					SceneContainer.getInstance().showDebug(SceneContainer.getInstance().debugCurrentPage - 1);
					break;
				case 34:
					SceneContainer.getInstance().showDebug(SceneContainer.getInstance().debugCurrentPage + 1);
					break;
			}
		}
		
		public function main():void
		{
			var param:Object = loaderInfo.parameters;
			var paramStr:String = "main:";
			for (var index:String in param)
			{
				paramStr += " " + index + "," + param[index];
			}
			SceneContainer.getInstance().debug(paramStr);
			if (param.hasOwnProperty("xn_sig_user"))
			{
				PlayerInfoHelper.getInstance().rrUserId = param["xn_sig_user"];
				Profile.rrSessionKey = param["xn_sig_session_key"];
				ServerHelper.getInstance().syncMeFirst(getUserPic);
			}
			else
			{
				if (Profile.DEBUG)
				{
					//本地调试用
					PlayerInfoHelper.getInstance().userId = 9;
					ServerHelper.getInstance().syncMeFirst(getMeAndNeighbors);
					//本地连接远程服务器调试用
//					PlayerInfoHelper.getInstance().rrUserId = 389770181;
//					Profile.rrSessionKey = "2.267e69f71cabd4761139b120310f8370.3600.1314622800-389770181";
//					ServerHelper.getInstance().syncMeFirst(getUserPic);
				}
				else
				{
					var messageBox:MessageBox = new MessageBox("您还没有授权我们的应用，请点击确定！");
					messageBox.buttonOK.addEventListener(MouseEvent.CLICK, redirectToWelcomePage);
					messageBox.x = (BFont.stageWidth - messageBox.width) / 2;
					messageBox.y = (BFont.stageHeight - messageBox.height) / 2;
					addChild(messageBox);
				}
			}
		}
		
		private function redirectToWelcomePage(event:MouseEvent):void
		{
			ExternalInterface.call("window.open", "http://apps.renren.com/longniaozhentan/welcome.php", "_parent");
		}
		
		private function getUserPic():void
		{
			SceneContainer.getInstance().debug("getUserPic");
			var rrApiHelper:RRApiHelper = new RRApiHelper();
			rrApiHelper.getUserPic(PlayerInfoHelper.getInstance().rrUserId, getFriends);
		}
		
		private function getFriends(rrTinyUrl:String, rrUserName:String):void
		{
			SceneContainer.getInstance().debug("getFriends");
			PlayerInfoHelper.getInstance().rrUserName = rrUserName;
			PlayerInfoHelper.getInstance().rrTinyUrl = rrTinyUrl;
			var rrApiHelper:RRApiHelper = new RRApiHelper();
			rrApiHelper.getFriends(PlayerInfoHelper.getInstance().rrUserId, getMeAndNeighbors);
		}
		
		private function getMeAndNeighbors():void
		{
			SceneContainer.getInstance().debug("getMeAndNeighbors");
			ServerHelper.getInstance().getMeAndNeighbors(Config.neighborNum, loadTask);
		}
		
		private function loadTask():void
		{
			SceneContainer.getInstance().debug("loadTask");
//			var players:Object = PlayerInfoHelper.getInstance().players;
//			var me:PlayerInfo = players[PlayerInfoHelper.getInstance().userId];
//			me.accomplishedFestivalTaskId = 0;
//			me.accomplishedTaskId = 3;
			var loadingScene:LoadingScene = new LoadingScene("正在加载任务资源……");
			SceneContainer.getInstance().loadScene(loadingScene);
			ResourceHelper.getInstance().startLoadTaskResource(loadingScene.loadBar.updateProgress, initMainScene);
		}
		
		private function initMainScene():void
		{
			SceneContainer.getInstance().debug("initMainScene");
			//判断玩家的动物
			var players:Object = PlayerInfoHelper.getInstance().players;
			var me:PlayerInfo = players[PlayerInfoHelper.getInstance().userId];
			if (me.ownAnimals.length == 0)
			{
				//开始新手导引
				SceneContainer.getInstance().startNewHand();
				SceneContainer.getInstance().loadScene(new FirstEntry());
			}
			else
			{
				SceneContainer.getInstance().loadScene(new MainEntry());
			}
		}
		
	}
}
